{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "import matplotlib.pylab as plt\n",
    "%matplotlib inline\n",
    "from matplotlib.pylab import rcParams\n",
    "rcParams['figure.figsize'] = 15, 6"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "air_passengers = pd.read_csv(\"./data/AirPassengers.csv\", header = 0, parse_dates = [0], names = ['Month', 'Passengers'], index_col = 0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "log_air_passengers = np.log(air_passengers.Passengers)\n",
    "log_air_passengers[1:10]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "log_air_passengers_diff = log_air_passengers - log_air_passengers.shift()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "log_air_passengers_diff.dropna(inplace=True)\n",
    "log_air_passengers_diff[1:10]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Let's talk about the ARIMA model\n",
    "# Auto-Regressive Integrated Moving Average\n",
    "# In this case we're talking about a series with dependence among values (more natural)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Nothing but a linear regression with a few times\n",
    "# 1. The number of Auto-Regressive Terms (p)\n",
    "# 2. The number of Moving Average Terms (q)\n",
    "# 3. The number of differences taken"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# We don't pull these out of thin air, but rather draw them from the data\n",
    "from statsmodels.tsa.stattools import acf, pacf\n",
    "lag_acf = acf(log_air_passengers_diff.values, nlags = 20)\n",
    "lag_pacf = pacf(log_air_passengers_diff.values, nlags = 20)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "plt.subplot(121) \n",
    "plt.plot(lag_acf)\n",
    "plt.axhline(y=0,linestyle='--')\n",
    "plt.axhline(y=-1.96/np.sqrt(len(log_air_passengers_diff)),linestyle='--')\n",
    "plt.axhline(y=1.96/np.sqrt(len(log_air_passengers_diff)),linestyle='--')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# look at where the plot crosses the upper confidence interval for the first time\n",
    "# for ACF this is 2 and gives us the p value"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "plt.subplot(121) \n",
    "plt.plot(lag_pacf)\n",
    "plt.axhline(y=0,linestyle='--')\n",
    "plt.axhline(y=-1.96/np.sqrt(len(log_air_passengers_diff)),linestyle='--')\n",
    "plt.axhline(y=1.96/np.sqrt(len(log_air_passengers_diff)),linestyle='--')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# look at where the plot crosses the upper confidence interval for the first time\n",
    "# for PACF this is 2 and gives us the p value"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from statsmodels.tsa.arima_model import ARIMA"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# AR model\n",
    "model = ARIMA(log_air_passengers, order=(2, 1, 0))  \n",
    "results_AR = model.fit(disp=-1)  \n",
    "plt.plot(log_air_passengers_diff)\n",
    "plt.plot(results_AR.fittedvalues, color='red')\n",
    "plt.title('RSS: %.4f'% sum((results_AR.fittedvalues-log_air_passengers_diff)**2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# MA model\n",
    "model = ARIMA(log_air_passengers, order=(0, 1, 2))  \n",
    "results_MA = model.fit(disp=-1)  \n",
    "plt.plot(log_air_passengers_diff)\n",
    "plt.plot(results_MA.fittedvalues, color='red')\n",
    "plt.title('RSS: %.4f'% sum((results_MA.fittedvalues-log_air_passengers_diff)**2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# ARIMA model\n",
    "model = ARIMA(log_air_passengers, order=(2, 1, 2))  \n",
    "results_ARIMA = model.fit(disp=-1)  \n",
    "plt.plot(log_air_passengers_diff)\n",
    "plt.plot(results_ARIMA.fittedvalues, color='red')\n",
    "plt.title('RSS: %.4f'% sum((results_ARIMA.fittedvalues-log_air_passengers_diff)**2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "predictions_ARIMA_diff = pd.Series(results_ARIMA.fittedvalues, copy=True)\n",
    "print(predictions_ARIMA_diff.head())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "predictions_ARIMA_diff_cumsum = predictions_ARIMA_diff.cumsum()\n",
    "print(predictions_ARIMA_diff_cumsum.head())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "predictions_ARIMA_log = pd.Series(log_air_passengers.ix[0], index=log_air_passengers.index)\n",
    "predictions_ARIMA_log = predictions_ARIMA_log.add(predictions_ARIMA_diff_cumsum,fill_value=0)\n",
    "predictions_ARIMA_log.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "predictions_ARIMA = np.exp(predictions_ARIMA_log)\n",
    "plt.plot(air_passengers)\n",
    "plt.plot(predictions_ARIMA)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  },
  "widgets": {
   "state": {},
   "version": "1.1.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
